<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <include file="./Public/Tpl/head.html" title="退货管理" description="药品管理系统." keywords="中南民族大学,药品管理系统"/>
</head>
<body>
<include file="./Public/Tpl/nav.html"/>
<div class="container">
  <div class="col-md-2">
    <div class="panel panel-default">
      <div class="panel-heading">功能</div>
      <div class="list-group list-menu">
        <button type="button" class="list-group-item list-menu active" data-to="0">药品退货</button>
        <button type="button" class="list-group-item list-menu" data-to="1" onclick="loadList(size,page)">退货记录</button>
      </div>
    </div>
  </div>
  <div class="col-md-10">
    <div class="panel panel-default func">
      <div class="panel-heading" style="text-align: center">退货</div>
      <div class="panel-body text-info"></div>
      <form id="retForm" class="form-horizontal">
        <div class="col-md-6">
          <div class="form-group">
            <label for="inputSellId" class="col-sm-3 control-label">销售码</label>
            <div class="col-sm-9" data-toggle="tooltip" data-placement="top" title="输入销售码自动获取销售信息">
              <input name="sell_id" type="text" class="form-control" id="inputSellId" placeholder="销售码"
                     autocomplete="off">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPrice" class="col-sm-3 control-label">销售单价</label>
            <div class="col-sm-9">
              <input name="price" type="number" class="form-control" id="inputPrice" disabled placeholder="0.00">
            </div>
          </div>
          <div class="form-group">
            <label for="inputAmount" class="col-sm-3 control-label">销售数量</label>
            <div class="col-sm-9">
              <input name="sell_amount" type="number" class="form-control" disabled id="inputAmount" placeholder="0">
            </div>
          </div>
          <div class="form-group">
            <label for="inputAllprice" class="col-sm-3 control-label">销售总价</label>
            <div class="col-sm-9">
              <input name="allprice" type="number" class="form-control" disabled id="inputAllprice" placeholder="0.00">
            </div>
          </div>
          <div class="form-group">
            <label for="inputTime" class="col-sm-3 control-label">销售时间</label>
            <div class="col-sm-9">
              <input name="time" type="text" class="form-control" id="inputTime" disabled placeholder="">
            </div>
          </div>
          <div class="form-group">
            <label for="inputRetAmount" class="col-sm-3 control-label">退货数量</label>
            <div class="col-sm-9">
              <input name="ret_amount" type="text" class="form-control" id="inputRetAmount" placeholder="未填写">
            </div>
          </div>
          <div class="form-group">
            <label for="inputRetPrice" class="col-sm-3 control-label">退货总额</label>
            <div class="col-sm-9">
              <input name="totalprice" type="text" class="form-control" id="inputRetPrice" placeholder="未填写">
            </div>
          </div>

        </div>
        <div class="col-md-6">
          <div class="form-group">
            <label for="inputName" class="col-sm-3 control-label">名称</label>
            <div class="col-sm-9">
              <input name="name" type="text" class="form-control" readonly id="inputName" placeholder="药品名称">
            </div>
          </div>
          <div class="form-group">
            <label for="inputSpec" class="col-sm-3 control-label">规格大小</label>
            <div class="col-sm-9">
              <input name="spec" type="text" class="form-control" disabled id="inputSpec" placeholder="规格">
            </div>
          </div>
          <div class="form-group">
            <label for="inputUnit" class="col-sm-3 control-label">计量单位</label>
            <div class="col-sm-9">
              <input name="unit" type="text" class="form-control" disabled id="inputUnit" placeholder="单位">
            </div>
          </div>
          <div class="form-group">
            <label for="inputFactory" class="col-sm-3 control-label">生产厂家</label>
            <div class="col-sm-9">
              <input name="factory" type="text" class="form-control" disabled id="inputFactory" placeholder="厂家">
            </div>
          </div>
          <div class="form-group">
            <label for="inputProduceDate" class="col-sm-3 control-label">生产日期</label>
            <div class="col-sm-9">
              <input name="producedate" type="text" class="form-control" id="inputProduceDate" disabled placeholder="">
            </div>
          </div>
          <div class="form-group">
            <label for="inputSeller" class="col-sm-3 control-label">销售员</label>
            <div class="col-sm-9">
              <input name="sell_by" type="text" class="form-control" disabled id="inputSeller" placeholder="">
            </div>
          </div>
          <div class="form-group">
            <label for="inputReason" class="col-sm-3 control-label">原因</label>
            <div class="col-sm-9">
              <input name="reason" type="text" class="form-control" id="inputReason" placeholder="未填写">
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-9">
            <button id="doReturn" type="button" class="btn btn-danger submit">退货</button>
            <button type="reset" class="btn btn-success">清空</button>
          </div>
        </div>
      </form>
    </div>
    <div class="panel panel-default func" id="returnList">
      <div class="panel-heading" style="text-align: center">退货记录</div>
      <table class="table table-hover">
        <thead>
        <tr>
          <th>退货编号</th>
          <th>销售编号</th>
          <th>药品名称</th>
          <th>规格</th>
          <th>单位</th>
          <th>单价</th>
          <th>退货数量</th>
          <th>退货总额</th>
          <th>退货日期</th>
          <th>操作员</th>
          <th>原因</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="info in list">
          <td>{{ info.ret_id }}</td>
          <td>{{ info.sell_id }}</td>
          <td>{{ info.name }}</td>
          <td>{{ info.spec }}</td>
          <td>{{ info.unit }}</td>
          <td>{{ info.sellprice }}</td>
          <td>{{ info.ret_amount }}</td>
          <td>{{ info.totalprice }}</td>
          <td>{{ info.time }}</td>
          <td>{{ info.realname }}</td>
          <td>{{ info.reason }}</td>
        </tr>
        </tbody>
      </table>
      <div id="returnPage" class="center">
        <nav>
          <ul class="pagination">
            <li v-for="i in count">
              <a href="javascript:" :data-to="i">{{ i }}</a>
            </li>
          </ul>
        </nav>
      </div>
    </div>
  </div>
</div>
<include file="./Public/Tpl/footer.html"/>
<script>
    var size = 10;
    var page = 1;
    var returnList = {list: [], count: 10};
    var app = new Vue({
        el: '#returnList',
        data: returnList
    });
    function loadList(size, page) {
        $.ajax({
            type: 'POST',
            url: "{:U('Return/getRetList')}",
            data: {"size": size, "page": page},
            dataType: 'json',
            async:false,
            success: function (rs) {
                if (rs.code == 200) {
                    returnList.list = rs.data.list;
                    returnList.count = Math.ceil(rs.data.count / size);
                }
            },
            beforeSend: function () {
                this.layerIndex = layer.load(1, {shade: [0.1, '#393D49']});
            },
            complete: function () {
                layer.close(this.layerIndex);
            }
        });
    }
    $(document).ready(function () {
        //加载更多
        layui.use(['laypage', 'layer'], function () {
            loadList(size,page);
            layer = layui.layer;
            laypage = layui.laypage;
            laypage({
                cont: 'returnPage'
                , pages: returnList.count
                , skin: '#337ab7'
                , skip: true,
                jump: function (obj, first) {
                    if (!first) {
                        page = obj.curr;
                        loadList(size, page);
                    }
                }
            });
        });
        //退货按钮
        $("#doReturn").on("click", function () {
            var amount = $("#inputRetAmount").val();
            var price = $("#inputRetPrice").val();
            if (amount == 0 || price == 0) {
                alert("退货数量和价格不能为空或为零!");
                return;
            }
            $.ajax({
                url: "{:U('Return/doAdd')}",
                data: $("#retForm").serialize(),
                datatype: "json",
                type: "POST",
                success: function (data) {
                    if (data['code'] == 200) {
                        layer.msg("退货成功!",{icon:1});
                        window.location.reload();
                    } else {
                        layer.msg("原因:" + data['msg'],{icon:2});
                    }
                },
                error: function () {
                    layer.msg("提交过程中发生问题! ",{icon:0});
                },
                beforeSend: function () {
                    this.layerIndex = layer.load(0, {shade: [0.1, '#393D49']});
                },
                complete: function () {
                    layer.close(this.layerIndex);
                }
            })
        });
        //填写完数量
        $("#inputRetAmount").on("keyup", function () {
            $("#inputRetPrice").attr("value", (this.value * $("#inputPrice").val()).toFixed(2));
        });
        //销售单事件
        $("#inputSellId").on("keyup", function () {
            $.ajax({
                url: "{:U('Sell/getInfo')}",
                data: "sell_id=" + $("#inputSellId").val(),
                datatype: "json",
                type: "POST",
                success: function (data) {
                    console.log(data);
                    if (data['code'] == 200 && data.data.sell_status != 1) {
                        var tempName = data.data.name;
                        var tempSpec = data.data.spec;
                        var tempUnit = data.data.unit;
                        var tempFactory = data.data.factory;
                        var tempProduceDate = formatDate(data.data.producedate);
                        var tempSeller = data.data.realname;
                        var tempPrice = data.data.price;
                        var tempAmount = data.data.sell_amount;
                        var tempAllprice = data.data.subtotal;
                        var tempTime = formatDate(data.data.time);
                    } else if (data['code'] == 200 && data.data.sell_status == 1) {
                        var tempName = tempSpec = tempUnit = tempFactory = tempProduceDate = tempSeller = tempPrice = tempAmount = tempAllprice = tempTime = "该销售单已经发生退货行为";
                    } else {
                        var tempName = tempSpec = tempUnit = tempFactory = tempProduceDate = tempSeller = tempPrice = tempAmount = tempAllprice = tempTime = data.msg;
                    }
                    $("#inputName").attr("value", tempName);
                    $("#inputSpec").attr("value", tempSpec);
                    $("#inputUnit").attr("value", tempUnit);
                    $("#inputFactory").attr("value", tempFactory);
                    $("#inputProduceDate").attr("value", tempProduceDate);
                    $("#inputSeller").attr("value", tempSeller);
                    $("#inputPrice").attr("value", tempPrice);
                    $("#inputAmount").attr("value", tempAmount);
                    $("#inputAllprice").attr("value", tempAllprice);
                    $("#inputTime").attr("value", tempTime);
                },
                error: function () {
                },
            })
        });
    });
</script>
</body>
</html>